package other.edan.newpaas.bid;

import com.alibaba.fastjson.JSONObject;
import com.rkhd.platform.sdk.api.annotations.RequestMethod;
import com.rkhd.platform.sdk.api.annotations.RestApi;
import com.rkhd.platform.sdk.api.annotations.RestMapping;
import com.rkhd.platform.sdk.api.annotations.RestQueryParam;
import com.rkhd.platform.sdk.data.model.BidManagement__c;
import com.rkhd.platform.sdk.data.model.Opportunity;
import com.rkhd.platform.sdk.exception.AsyncTaskException;
import com.rkhd.platform.sdk.exception.ScriptBusinessException;
import com.rkhd.platform.sdk.log.Logger;
import com.rkhd.platform.sdk.log.LoggerFactory;
import com.rkhd.platform.sdk.model.OperateResult;
import com.rkhd.platform.sdk.model.XObject;
import com.rkhd.platform.sdk.trigger.DataResult;
import com.rkhd.platform.sdk.trigger.Trigger;
import com.rkhd.platform.sdk.trigger.TriggerRequest;
import com.rkhd.platform.sdk.trigger.TriggerResponse;
import other.edan.newpaas.utils.HCXObjectService;
import other.edan.newpaas.utils.ResultVo;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description 销售机会生成投标管理
 * @Author 黎炎松
 * 2024-10-12 14:14
 */
@RestApi(baseUrl = "/edan/bid")
public class CreateBidManagetByOpt implements Trigger {

    private static final Logger LOGGER = LoggerFactory.getLogger();
    private static String LOGGER_MSG = "销售机会生成投标管理 ==> ";

    /**
     * tregger 触发事件
     * @param request
     * @return
     * @throws ScriptBusinessException
     */
    @Override
    public TriggerResponse execute(TriggerRequest request) throws ScriptBusinessException {
        LOGGER.debug(LOGGER_MSG + "触发开始");
        List<XObject> dataList = request.getDataList();
        List<DataResult> dataResultList = new ArrayList<>();
        List<XObject> objectList = new ArrayList<>();
        for (XObject xObject : dataList) {
            objectList.add(xObject);
            dataResultList.add(new DataResult(true, "", xObject));
        }
        Opportunity opportunity = (Opportunity) objectList.get(0);
        createBidManage(opportunity);
        return new TriggerResponse(true, "处理完成", dataResultList);
    }

    /**
     * 手动触发生成投标管理
     * @param
     * @return
     */
    @RestMapping(value = "/createBidManage", method = RequestMethod.POST)
    public ResultVo createBidManage(@RestQueryParam(name = "id") Long optId) throws AsyncTaskException {
        LOGGER.info("手动触发生成投标管理");
        Opportunity opportunity = new Opportunity();
        opportunity.setId(optId);
        opportunity = HCXObjectService.get(opportunity);
        return createBidManage(opportunity);
    }

    public static void main(String[] args) throws Exception {
        Opportunity opportunity = new Opportunity();
        opportunity.setId(3499513958554247L);
        opportunity = HCXObjectService.get(opportunity);
        new CreateBidManagetByOpt().createBidManage(opportunity);
    }


    /**
     * 根据销售机会生成投标管理
     * @param opportunity
     */
    public ResultVo createBidManage(Opportunity opportunity) {
        try {
            LOGGER.info(LOGGER_MSG + "开始处理");

            // 创建投标管理
            BidManagement__c bidManagement = new BidManagement__c();
            bidManagement.setEntityType(3421587888429740L);
            bidManagement.setApplyUser__c(opportunity.getOwnerId()); // 申请人
            bidManagement.setTerminal__c(opportunity.getAccountId()); // 终端医院

            // 获取申请人的申请部门、业务线
            String sql = "select id,dimDepart,dimDepart.tenderApplicationDepart__c tenderApplicationDepart,dimDepart.appBusinessLine__c appBusinessLine " +
                    " from user where id = " + opportunity.getOwnerId();
            List<JSONObject> userList = HCXObjectService.queryAllUseXoql(sql);
            if (userList.size() > 0) {
                JSONObject user = userList.get(0);
                Long tenderApplicationDepart = user.getLong("tenderApplicationDepart");
                bidManagement.setApplyDepartment__c(tenderApplicationDepart != null ? tenderApplicationDepart : user.getLong("dimDepart")); // 申请部门
                bidManagement.setAppBusinessLine__c(user.getInteger("appBusinessLine")); // 申请人业务线
            }

            // 保存
            OperateResult insert = HCXObjectService.insert(bidManagement);
            System.out.println("insert = " + insert.getDataId());
            if (!insert.getSuccess()) {
                LOGGER.error(LOGGER_MSG + "创建投标管理异常: " + insert.getErrorMessage());
                return ResultVo.error("创建投标管理异常: " + insert.getErrorMessage());
            }

            // 设置投标管理编号
            bidManagement.setId(insert.getDataId());
            new CreateBidManagetCode().generateCode(bidManagement);

            LOGGER.info(LOGGER_MSG + "处理成功");
            return ResultVo.success("处理成功");
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error(LOGGER_MSG + "处理异常: " + e.getMessage());
            return ResultVo.error("处理异常: " + e.getMessage());
        }
    }


}
